package org.leaf.pethome.business.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.leaf.pethome.business.entity.PetUser;
import org.leaf.pethome.business.mapper.PetUserMapper;
import org.leaf.pethome.common.entity.Message;
import org.springframework.stereotype.Service;

@Service
public class PetUserService extends ServiceImpl<PetUserMapper, PetUser> {

    public Message userSignIn(PetUser petUser){
        LambdaQueryWrapper<PetUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PetUser::getUserName, petUser.getUserName());
        if(super.count(wrapper) > 0){
            return Message.repeat("用户名已存在");
        }
        try{
            super.save(petUser);
            return Message.succeed("注册成功");
        }catch (Exception e){
            return Message.error(e);
        }
    }

    public Message userLogin(PetUser petUser){
        LambdaQueryWrapper<PetUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PetUser::getUserName, petUser.getUserName());
        PetUser petUserSrc =super.getOne(wrapper);
        if(null == petUserSrc || !petUserSrc.getUserPasswd().equals(petUser.getUserPasswd())) {
            return Message.fail("用户或密码错误");
        }
        return Message.succeed(petUserSrc);
    }

    public Message show(){
        return Message.succeed(super.getBaseMapper().show());
    }
}
